#ifndef CAMERADECKLINK_H
#define CAMERADECKLINK_H

//#import <Cocoa/Cocoa.h>

#include "common.h"

#include "DeckLinkAPI.h"

#include <opencv/cv.h>

class DeckLinkCaptureDelegate;

class CameraDecklink
{
public:
    CameraDecklink();
    ~CameraDecklink();

    bool initializeCamera(IDeckLink * _deckLink);
    bool startCapture();
    void stopCapture();

    IplImage*   captureLastFrame();
    cv::Mat     captureLastCvMat();

    static QString toQString(CFStringRef str);

    bool fetchSelectedDisplayMode(int modeIndex);

private:
    DeckLinkCaptureDelegate*        m_Delegate;
    IDeckLinkInput*                 m_DeckLinkInput;
    IDeckLinkDisplayMode*           m_DisplayMode;
    BMDVideoInputFlags              m_InputFlags;
    BMDDisplayMode                  m_SelectedDisplayMode;
    BMDPixelFormat                  m_PixelFormat;
    int                             m_ExitStatus;
    HRESULT                         m_Result;
};

class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
{
public:
    DeckLinkCaptureDelegate();
    ~DeckLinkCaptureDelegate();

    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
    virtual ULONG STDMETHODCALLTYPE AddRef(void);
    virtual ULONG STDMETHODCALLTYPE  Release(void);
    virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents,
                                                              IDeckLinkDisplayMode*,
                                                              BMDDetectedVideoInputFormatFlags);
    virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*,
                                                             IDeckLinkAudioInputPacket*);

    IplImage* getLastImage();

    void convertFrameToOpenCV(void* frameBytes, IplImage * m_RGB);

    pthread_mutex_t		*sleepMutex;
    pthread_cond_t		*sleepCond;
            bool stopped;
    //        bool converting;
    void* frameBytes;
    long frameCount;
    BMDTimecodeFormat		g_timecodeFormat;

private:
    ULONG				m_refCount;
    pthread_mutex_t		m_mutex;
    IplImage*           lastImage;

    int height, width;
};

#endif // CAMERADECKLINK_H
